"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.defaultRenderLogoAndTitle = exports.defaultRenderLogo = exports.defaultRenderCollapsedButton = exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = require("react");

var _jsxRuntime = require("react/jsx-runtime");

var _icons = require("@ant-design/icons");

var _antdV = require("antd-v4");

var _classnames = _interopRequireDefault(require("classnames"));

var _BaseMenu = _interopRequireDefault(require("./BaseMenu"));

var _Counter = _interopRequireDefault(require("./Counter"));

require("./index.less");

var Sider = _antdV.Layout.Sider;

var defaultRenderLogo = function defaultRenderLogo(logo) {
  if (typeof logo === 'string') {
    return (0, _jsxRuntime.jsx)("img", {
      src: logo,
      alt: "logo"
    });
  }

  if (typeof logo === 'function') {
    return logo();
  }

  return logo;
};

exports.defaultRenderLogo = defaultRenderLogo;

var defaultRenderLogoAndTitle = function defaultRenderLogoAndTitle(props) {
  var renderKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'menuHeaderRender';
  var logo = props.logo,
      title = props.title,
      layout = props.layout;
  var renderFunction = props[renderKey || ''];

  if (renderFunction === false) {
    return null;
  }

  var logoDom = defaultRenderLogo(logo);
  var titleDom = (0, _jsxRuntime.jsx)("h1", {
    children: title !== null && title !== void 0 ? title : 'Ant Design Pro'
  });

  if (renderFunction) {
    // when collapsed, no render title
    return renderFunction(logoDom, props.collapsed ? null : titleDom, props);
  }

  if (layout === 'mix' && renderKey === 'menuHeaderRender') {
    return null;
  }

  return (0, _jsxRuntime.jsxs)("a", {
    children: [logoDom, props.collapsed ? null : titleDom]
  });
};

exports.defaultRenderLogoAndTitle = defaultRenderLogoAndTitle;

var defaultRenderCollapsedButton = function defaultRenderCollapsedButton(collapsed) {
  return collapsed ? (0, _jsxRuntime.jsx)(_icons.MenuUnfoldOutlined, {}) : (0, _jsxRuntime.jsx)(_icons.MenuFoldOutlined, {});
};

exports.defaultRenderCollapsedButton = defaultRenderCollapsedButton;

var SiderMenu = function SiderMenu(props) {
  var _classNames;

  var collapsed = props.collapsed,
      fixSiderbar = props.fixSiderbar,
      menuFooterRender = props.menuFooterRender,
      _onCollapse = props.onCollapse,
      theme = props.theme,
      siderWidth = props.siderWidth,
      isMobile = props.isMobile,
      onMenuHeaderClick = props.onMenuHeaderClick,
      _props$breakpoint = props.breakpoint,
      breakpoint = _props$breakpoint === void 0 ? 'lg' : _props$breakpoint,
      style = props.style,
      layout = props.layout,
      _props$menuExtraRende = props.menuExtraRender,
      menuExtraRender = _props$menuExtraRende === void 0 ? false : _props$menuExtraRende,
      _props$collapsedButto = props.collapsedButtonRender,
      collapsedButtonRender = _props$collapsedButto === void 0 ? defaultRenderCollapsedButton : _props$collapsedButto,
      links = props.links,
      menuContentRender = props.menuContentRender,
      prefixCls = props.prefixCls,
      onOpenChange = props.onOpenChange,
      headerHeight = props.headerHeight,
      logoStyle = props.logoStyle;
  var baseClassName = "".concat(prefixCls, "-sider");

  var _MenuCounter$useConta = _Counter.default.useContainer(),
      flatMenuKeys = _MenuCounter$useConta.flatMenuKeys;

  var siderClassName = (0, _classnames.default)("".concat(baseClassName), (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(baseClassName, "-fixed"), fixSiderbar), (0, _defineProperty2.default)(_classNames, "".concat(baseClassName, "-layout-").concat(layout), layout && !isMobile), (0, _defineProperty2.default)(_classNames, "".concat(baseClassName, "-light"), theme !== 'dark'), _classNames));
  var headerDom = defaultRenderLogoAndTitle(props);
  var extraDom = menuExtraRender && menuExtraRender(props);
  var menuDom = menuContentRender !== false && flatMenuKeys && /*#__PURE__*/(0, _react.createElement)(_BaseMenu.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
    key: "base-menu",
    mode: "inline",
    handleOpenChange: onOpenChange,
    style: {
      width: '100%'
    },
    className: "".concat(baseClassName, "-menu")
  }));
  var menuRenderDom = menuContentRender ? menuContentRender(props, menuDom) : menuDom;
  var linksMenuItems = (links || []).map(function (node, index) {
    return {
      className: "".concat(baseClassName, "-link"),
      label: node,
      key: index
    };
  });

  if (collapsedButtonRender && !isMobile) {
    linksMenuItems.push({
      className: "".concat(baseClassName, "-collapsed-button"),
      //@ts-ignore
      title: false,
      key: 'collapsed',
      onClick: function onClick() {
        if (_onCollapse) {
          _onCollapse(!collapsed);
        }
      },
      label: collapsedButtonRender(collapsed)
    });
  }

  return (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [fixSiderbar && (0, _jsxRuntime.jsx)("div", {
      style: (0, _objectSpread2.default)({
        width: collapsed ? 48 : siderWidth,
        overflow: 'hidden',
        flex: "0 0 ".concat(collapsed ? 48 : siderWidth, "px"),
        maxWidth: collapsed ? 48 : siderWidth,
        minWidth: collapsed ? 48 : siderWidth,
        transition: "background-color 0.3s, min-width 0.3s, max-width 0.3s cubic-bezier(0.645, 0.045, 0.355, 1)"
      }, style)
    }), (0, _jsxRuntime.jsxs)(Sider, {
      collapsible: true,
      trigger: null,
      collapsed: collapsed,
      breakpoint: breakpoint === false ? undefined : breakpoint,
      onCollapse: function onCollapse(collapse) {
        if (isMobile) return;
        _onCollapse === null || _onCollapse === void 0 ? void 0 : _onCollapse(collapse);
      },
      collapsedWidth: 48,
      style: (0, _objectSpread2.default)({
        overflow: 'hidden',
        paddingTop: layout === 'mix' && !isMobile ? headerHeight : undefined
      }, style),
      width: siderWidth,
      theme: theme,
      className: siderClassName,
      children: [headerDom && (0, _jsxRuntime.jsx)("div", {
        className: (0, _classnames.default)("".concat(baseClassName, "-logo"), (0, _defineProperty2.default)({}, "".concat(baseClassName, "-collapsed"), collapsed)),
        onClick: layout !== 'mix' ? onMenuHeaderClick : undefined,
        id: "logo",
        style: logoStyle,
        children: headerDom
      }), extraDom && (0, _jsxRuntime.jsx)("div", {
        className: "".concat(baseClassName, "-extra ").concat(!headerDom && "".concat(baseClassName, "-extra-no-logo")),
        children: extraDom
      }), (0, _jsxRuntime.jsx)("div", {
        style: {
          flex: 1,
          overflowY: 'auto',
          overflowX: 'hidden'
        },
        children: menuRenderDom
      }), (0, _jsxRuntime.jsx)("div", {
        className: "".concat(baseClassName, "-links"),
        children: (0, _jsxRuntime.jsx)(_antdV.Menu, {
          theme: theme,
          inlineIndent: 16,
          className: "".concat(baseClassName, "-link-menu"),
          selectedKeys: [],
          openKeys: [],
          mode: "inline",
          items: linksMenuItems
        })
      }), menuFooterRender && (0, _jsxRuntime.jsx)("div", {
        className: (0, _classnames.default)("".concat(baseClassName, "-footer"), (0, _defineProperty2.default)({}, "".concat(baseClassName, "-footer-collapsed"), !collapsed)),
        children: menuFooterRender(props)
      })]
    })]
  });
};

var _default = SiderMenu;
exports.default = _default;